package com.example.travel_front.Activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.travel_front.Fragment.ChatFragment;
import com.example.travel_front.Fragment.DiscoverFragment;
import com.example.travel_front.Fragment.FeedFragment;
import com.example.travel_front.Fragment.HomeFragment;
import com.example.travel_front.Fragment.SettingsFragment;
import com.example.travel_front.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class HomeActivity extends AppCompatActivity {

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            int itemId = item.getItemId();

            if (itemId == R.id.navigation_home) {
                selectedFragment = new HomeFragment();
            } else if (itemId == R.id.navigation_discover) {
                selectedFragment = new DiscoverFragment();
            } else if (itemId == R.id.navigation_feed) {
                selectedFragment = new FeedFragment();}
            else if (itemId == R.id.navigation_chat) {
                    selectedFragment = new ChatFragment();}
             else if (itemId == R.id.navigation_settings) {
                selectedFragment = new SettingsFragment();
            }

            if (selectedFragment != null) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
            }

            return true;

        });

         // 设置默认显示的 Fragment
         if (savedInstanceState == null) {
             getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
             bottomNavigationView.setSelectedItemId(R.id.navigation_home);
         }
     }

    @Override
    public void onBackPressed() {
        // Fragment 返回逻辑
        int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count == 0) {
            super.onBackPressed();
        } else {
            getSupportFragmentManager().popBackStack();
        }
    }
}
